package com.itheima.test;

import com.itheima.domain.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/*
    数据库操作 基于Mybatis
 */
public class StudentController {
    //成员变量
    InputStream is = null;
    SqlSession session = null;

    //注解@Before 在@Test运行前运行
    @Before
    public void before() throws IOException {
        //读取核心配置文件
        is = Resources.getResourceAsStream("MyBatisConfig.xml");
        //构建Sql会话工厂 对象
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //获取Sql会话
        session = factory.openSession();
    }

    //注解@After 在@Test运行后运行
    @After
    public void after() throws IOException {
        //提交事务
        session.commit();
        //释放资源
        is.close();
        session.close();
    }

    //查询所有学生
    @Test
    public void findAll() {
        //调用statement对象并执行: namespace.statement
        List<Student> list = session.selectList("StudentMapper.selectAll");
        for (Student student : list) {
            System.out.println(student);
        }
    }

    //通过id查找学生
    @Test
    public void findById() {
        //调用statement对象并执行
        Student stu = session.selectOne("StudentMapper.findById", 3);
        System.out.println(stu);

    }


    //统计学生数量
    @Test
    public void countAll() {
        System.out.println("学生总数为: " + (Integer)session.selectOne("StudentMapper.countAll"));
    }

    //统计男生和女生的数量
    @Test
    public void countSex(){
        List<Map> maps = session.selectList("StudentMapper.countSex");
        System.out.println(maps);
        //重新封装数据
        Map sexMap = new HashMap<>();
        for (Map map : maps) {
            String gender = (String)map.get("gender");
            Long total = (Long)map.get("total");
            sexMap.put(gender,total);
        }
        //遍历
        Set keys = sexMap.keySet();
        for (Object key : keys) {
            System.out.println(key +": " +  sexMap.get(key));
        }
    }

    //通过性别和年龄查找学生  假设唯一
    @Test
    public void findByGenderAndAge(){
        //创建性别和年龄的map
        Map map = new HashMap<>();
        map.put("gender","男");
        map.put("age",25);
        //获取执行者对象并执行
        Student stu = session.selectOne("StudentMapper.findByGenderAndAge", map);
        System.out.println(stu);
    }

    //根据id更新用户信息
    @Test
    public void updateStudent(){
        //获取要修改的学生
        Student stu = session.selectOne("StudentMapper.findById",2);
        System.out.println(stu);
        stu.setName("李四四");
        //获取执行者对象并修改学生
        session.update("StudentMapper.updateStudent",stu);
        //查看一下
        Student stu1 = session.selectOne("StudentMapper.findById", 2);
        System.out.println(stu1);
    }


    //根据用户名模糊查询
    @Test
    public void findLikeName(){
        //查找学生并返回
//        List<Student> list = session.selectList("StudentMapper.findLikeName", "%三%");
        List<Student> list = session.selectList("StudentMapper.findLikeName", "三");
        //打印结果
        for (Student student : list) {
            System.out.println(student);
        }
    }


    //获取保存后主键列的值
    @Test
    public void insertStu(){
        Student stu = new Student(null, "周七", 28, "女");
        System.out.println("保存前: "+stu);
        //添加学生
        session.insert("StudentMapper.saveUser",stu);

        System.out.println("保存后: "+stu);
    }

}
